Ext.namespace('AlUtil');

g_loadMask = null;

AlUtil.firstLowerCase = function(value)
{
	var val = value;
	if(typeof val == 'string'){

		if (val.length >0)
		{
			val = val.substring(0, 1).toLowerCase()
			+ val.substring(1, val.length);
		}
	}
	return val;
};

AlUtil.firstUpperCase = function(value)
{
	var val = value;
	if(typeof val == 'string'){

		if (val.length >0)
		{
			val = val.substring(0, 1).toUpperCase()
			+ val.substring(1, val.length);
		}
	}
	return val;
};
	
AlUtil.getShortValue = function(value,sep)
{
	var val = value;
	var seqTmp = sep || '.';
	if(typeof val == 'string'){

		if (val.length >0)
		{
			while(val.indexOf(seqTmp)>0)
			{
				val = val.substring(val.indexOf(seqTmp)+1,val.length);
			}
		}
	}
	return val;
};

/**
 * 用于grid自动换行
 */
AlUtil.gridAutoNewLineRenderer = function(value, metadata, record)
{
	metadata.attr = 'style="white-space:normal;"';
	return "<div style='vertical-align:left;height:26px;'>"+value+"</div>";
};

/**
 * 判断value是否为Integer类型
 * 
 * @param {}
 *            value
 * @return {}
 */
AlUtil.isInteger = function(value)
{
    var reg = /^\d+$/;
    return reg.test(value);
};

/**
 * 取得参数的值(为空或0值返回空串)
 * 
 * @param {}
 *            value
 * @return {String}
 */
AlUtil.getParamValue = function(value)
{
    if (Ext.isEmpty(value) || value == 0)
    {
        return "";
    }
    return value;
};

/**
 * 弹出提示框
 * 
 * @param {}
 *            msg
 * @param {}
 *            title
 * @param {}
 *            type
 * @param {}
 *            buttons
 */
AlUtil.showMsg = function(msg, title, type, buttons,fn)
{
   var msgType;
	if (Ext.isEmpty(msg))
    {
        msg = _('loading...');
    }
    if (Ext.isEmpty(title))// 默认为提示框
    {
        title = _('prompt');
    }
    if (Ext.isEmpty(type))
    {
        type = Ext.MessageBox.INFO;
    }
    if (Ext.isEmpty(buttons))
    {
        buttons = Ext.MessageBox.OK;
    }
    switch (type)
    {
        case 'INFO' :
            msgType = Ext.MessageBox.INFO;
            break;
        case 'WARN' :
            msgType = Ext.MessageBox.WARNING;
            break;
        default :
            msgType = type;
            break;
    }

    Ext.MessageBox.show(
            {
                msg : msg,
                title : title,
                icon : msgType,
                //closable : false,
                buttons : buttons,
                fn : fn
            });
};

/**
 * 消息等待...
 * 
 * @param {}
 *            msg
 * @param {}
 *            body
 */
AlUtil.wait = function(msg, body)
{
	AlUtil.resume();
    
    if (Ext.isEmpty(msg))
    {
        msg = _("processing, please wait...");
    }
    if (Ext.isEmpty(body))
    {
        body = Ext.getCmp('frame-mainCenter').getActiveTab();
    }

    g_loadMask = new top.Ext.LoadMask(body,
            {
                msg : msg,
                removeMask : true
            });
    g_loadMask.show();
};

/**
 * 继续执行，在等待的事务完成后清除等待框,与方法com.freesky.em8.gl.common.wait配合使用
 */
AlUtil.resume = function()
{
    if (g_loadMask != null)
    {
        g_loadMask.hide();
        g_loadMask = null;
    }
};

//判断传入的val值是否为空字符串或null、undefined
AlUtil.isNullOrEmpty = function(val)
{
	if(typeof val == 'string'){
		var trimRe = /^\s+|\s+$/g;
		val = String(val).replace(trimRe, "");
	}
	return val === null || val === undefined || val === '';
};

//为传入的value值进行四舍五入计算并保留pos位小数
AlUtil.formatFloat = function(value,pos)
{
	return Math.round(value*Math.pow(10, pos))/Math.pow(10, pos);
};
//给传入的value字符串染成传入的color颜色
AlUtil.formatColor = function(value, color)
{	
	color = color || 'red';
	if(typeof value !== 'string')
		value = String.valueOf(value);

	return "<font color=" + color + ">" + value + "</font>";
};
//arguments[0] <=> count 第一个参数是需要生成空格的数量
//arguments[1] <=> value 第二个参数是需要给传入的value两边加count数量的空格(如果传入该参数的情况下)
AlUtil.getBlankSpace = function()
{
	var a = arguments, s = "";
	if(a.length < 1 || a[0] < 1)return s;		
	while(a[0] > 0){
		s += "&nbsp;";
		a[0]--;
	}
	if(a.length > 1) return s + a[1] + s;
	return s;
};
	
//是否空数组
AlUtil.isEmptyArray = function(array){
	if(Ext.isArray(array)){
		return array.length > 0 ? false : true;
	}else if(typeof(array) == 'string'){
		if(array == '[]'){
			return true;
		}else{
			return false;
		}
	}else{
		AlUtil.showMsg(_('array is invalid!'));
	}
};
//是否空对象
AlUtil.isEmptyObject = function(obj){
	if(typeof(obj) == 'object'){
		var i = 0;
		for(var key in obj){
			i++; break;
		}
		return i > 0 ? false : true;
	}else if(typeof(obj) == 'string'){
		if(obj == '{}'){
			return true;		
		}else{
			return false;
		}
	}else{
		AlUtil.showMsg(_('object is invalid!'));
	}
};
